<template>
  <div class="login">
    <div class="main">
      <el-form :model="user" ref="user" label-width="auto">
        <el-form-item
          prop="username"
          :rules="[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            {
              message: '用户名长度必须为5-11位',
              trigger: ['blur', 'change'],
              pattern: /^.{5,11}$/,
            },
          ]"
        >
          <el-input
            v-model="user.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          :rules="[
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur',
            },
            {
              message: '密码长度不能小于5位',
              trigger: ['blur', 'change'],
              pattern: /^.{5,}$/,
            },
          ]"
        >
          <el-input
            v-model="user.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submitForm('user')">登录</el-button>
          <el-button @click="resetForm('user')">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
    </div>
  </div>
</template>
<script>
import { setToken } from "@/utils/storage";
export default {
  data() {
    return {
      user: {
        username: "admin",
        password: "123456",
      },
    }; 
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch("user/login", this.user).then((res) => {
            if (res.data === null) {
              this.$message.error(res.meta.msg);
            } else {
              this.$message({
                message: "登陆成功",
                type: "success",
              });
              setToken(res.data.token);
              this.$router.push("/");
            }
          });
        } else {
          this.$message.error("请输入正确的用户名和密码");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang='less'  scoped>
.login {
  width: 100vw;
  height: 100vh;
  background-color: #2b4b6b;
  position: relative;
  .main {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    .el-form {
      width: 100%;
      margin-top: 114px;
      margin-bottom: 22px;
      padding: 0 20px;
      .btn {
        display: flex;
        justify-content: flex-end;
      }
    }
    .logo {
      width: 140px;
      height: 140px;
      padding: 10px;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 10px solid #fff;
      border-radius: 50%;
      background-color: #eee;
      box-shadow: 5px 5px 5px #ccc;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
  }
}
</style>
